package org.vuzya.motivateme.activities;

import static org.vuzya.motivateme.helpers.ConstantHelper.EXTRA_BUSINESS_ID;
import static org.vuzya.motivateme.helpers.ConstantHelper.EXTRA_MOTIVATION_ANSWERS;

import org.vuzya.motivateme.R;
import org.vuzya.motivateme.db.DBHelper;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Camera;
import android.graphics.Matrix;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Transformation;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

public class TestActivity extends Activity {
	public static String EXTRA_MOTIVATION_ID = "motivationId";
	
	private String[] questionTitles = null;
    private String[] questionDescriptions = null;
    private String[] answers = null;
    private int questionNumber = 0;
	
    private TextView titleTextView;
    private TextView descriptionTextView;
    private EditText answerEditText;
    //private Button nextButton;
    private Button backButton;
    
    private Animation forwardAnim;
    private Animation backwardAnim;
    private LinearLayout rootLayout;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
    
        titleTextView = (TextView)findViewById(R.id.test_title_textview);
        descriptionTextView = (TextView)findViewById(R.id.test_description_textview);
        answerEditText = (EditText)findViewById(R.id.test_answer_edittext);
        backButton = (Button)findViewById(R.id.test_back_button);
        //nextButton = (Button)findViewById(R.id.test_next_button);
        rootLayout = (LinearLayout)findViewById(R.id.test_root_layout);
        
        Resources res = getResources();
        questionTitles = res.getStringArray(R.array.question_titles);
        questionDescriptions = res.getStringArray(R.array.question_descriptions);
        answers = new String[questionTitles.length];
                
        forwardAnim = new MyTurnRoundAnimation(true);
        backwardAnim = new MyTurnRoundAnimation(false);
        
        bindViews();
    }

	private void bindViews() {
		titleTextView.setText(questionTitles[questionNumber]);
		descriptionTextView.setText(questionDescriptions[questionNumber]);
		answerEditText.setText(answers[questionNumber]);
	}
	
	public void nextClicked(View v){
		if(questionNumber==0){
			backButton.setEnabled(true);
		}
		answers[questionNumber] = answerEditText.getText().toString();
		
		questionNumber++;

		if(questionNumber == answers.length){
			long businessId = getIntent().getLongExtra(EXTRA_BUSINESS_ID, -1);
			DBHelper db = new DBHelper(this);
			long mid = db.addMotivation(businessId, answers);
			
			Intent i = new Intent(this, SummaryActivity.class);
			i.putExtra(EXTRA_MOTIVATION_ID, mid);
			i.putExtra(EXTRA_MOTIVATION_ANSWERS, answers);
			startActivity(i);
			finish();
			return;
		}
		
		rootLayout.startAnimation(forwardAnim);
		new DeleyedTask().execute();
		//bindViews();
	}
	
	public void backClicked(View v){
		if(questionNumber==1){
			backButton.setEnabled(false);
		}
		answers[questionNumber] = answerEditText.getText().toString();
		
		questionNumber--;
		
		rootLayout.startAnimation(backwardAnim);
		new DeleyedTask().execute();
		//bindViews();
	}
	
	public static class MyTurnRoundAnimation extends Animation{
    	private float centerX, centerY;
    	private boolean forward;
    	private static final Camera camera = new Camera();
    	
    	public MyTurnRoundAnimation(boolean forward){
    		this.forward = forward;
    	}
    	
    	@Override
    	public void initialize(int width, int height, int parentWidth, int parentHeight) {
    		super.initialize(width, height, parentWidth, parentHeight);
    		
    		centerX = width / 2.0f;
    		centerY = height / 2.0f;
    		
    		setDuration(1000);
    		setFillAfter(true);
    		//setInterpolator(i)
    	}
    	
    	@Override
    	protected void applyTransformation(float interpolatedTime, Transformation t) {
    		//super.applyTransformation(interpolatedTime, t);
    		final Matrix matrix = t.getMatrix();
    		
    		camera.save();
    		
    		camera.translate(0.0f, 0.0f, (float)(1000.0 * Math.sin(Math.PI * interpolatedTime)));
    		if(forward){
        		camera.rotateY(360.0f * interpolatedTime);
    		}else{
            	camera.rotateY(360.0f * (1 - interpolatedTime));
    		}
    		camera.getMatrix(matrix);
    		
    		//matrix.setScale(interpolatedTime, interpolatedTime);
    		
    		matrix.preTranslate(-centerX, -centerY);
    		matrix.postTranslate(centerX, centerY);
    		
    		camera.restore();
    	}
    }
	
	public class DeleyedTask extends AsyncTask<Void, Void, Void>{

		protected Void doInBackground(Void... params) {
			try {
				Thread.sleep(250);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			return null;
		}
		
		@Override
		protected void onPostExecute(Void result) {
			bindViews();
		}
	}
}
